<template>
  <div id="edit">
    <el-container>
      <el-header>
        <el-row>
          <el-col :span="4"><div class="grid-content bg-purple"></div></el-col>
          <el-col :span="4"><div class="grid-content bg-purple"></div></el-col>
          <el-col :span="4"><div class="grid-content bg-purple"></div></el-col>
          <el-col :span="4"><div class="grid-content bg-purple"></div></el-col>
          <el-col :span="4"><div class="grid-content bg-purple"></div></el-col>
          <el-col :span="4">
            <div class="grid-content bg-purple">
              <el-button type="info" round @click="saveAsDraft"
                >保存草稿</el-button
              >
              <el-button type="info" round @click="publish">发布文章</el-button>
            </div>
          </el-col>
        </el-row>
      </el-header>
      <el-main>
        <mavon-editor
          v-model="content"
          ref="md"
          @change="change"
          @save="saveAsDraft"
          :ishljs="true"
          style="
            min-height: calc(100vh - 120px);
            max-height: calc(100vh - 120px);
          "
        />
      </el-main>
    </el-container>
    <el-dialog
      title="发布文章"
      :visible.sync="dialogVisible"
      width="30%"
      v-bind:close-on-click-modal="false"
    >
      <el-form ref="form" :model="article" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="article.title"></el-input>
        </el-form-item>
        <el-form-item label="文章分类" prop="classifyId">
          <el-select
            v-model="article.classify.id"
            placeholder="请选择分类"
            v-on:change="classifySelectChange"
          >
            <el-option
              v-for="item in classify"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章标签">
          <template v-if="tags.length == 0">暂无</template>
          <el-checkbox-group v-model="article.tags">
            <el-checkbox
              v-for="item in tags"
              :key="item.id"
              :label="item.id"
              name="tag"
              >{{ item.tagName }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="是否原创">
          <el-radio-group v-model="article.fromType">
            <el-radio label="0">原创</el-radio>
            <el-radio label="1">转载</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="文章描述" prop="description">
          <el-input type="textarea" v-model="article.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取 消 </el-button>
        <el-button type="primary" @click="submit"> 发布 </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
import _ from "lodash";
export default {
  // 注册
  components: {
    mavonEditor,
  },
  data() {
    return {
      content: "", // 输入的markdown
      html: "", // 及时转的html
      dialogVisible: false, //发布文章时的form表单对话框
      saveAction: 0, //动作类型 1、保存草稿，2、发布
      article: {
        id: "",
        title: "",
        classify: {
          id: "",
        },
        tags: [],
        description: "",
        fromType: "0",
      },
      classify: [],
      tags: [],
      rules: {
        title: [
          { required: true, message: "请输入文章标题", trigger: "blur" },
          {
            min: 2,
            max: 100,
            message: "长度在 2 到 100 个字符",
            trigger: "blur",
          },
        ],
        classifyId: [
          { required: true, message: "请选择分类", trigger: "change" },
        ],
        description: [
          { required: true, message: "请填写文章描述", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 所有操作都会被解析重新渲染
    change(value, render) {
      // render 为 markdown 解析后的结果[html]
      this.html = render;
    },
    saveAsDraft() {
      this.saveAction = 1;
      this.save();
    },
    publish() {
      this.saveAction = 2;
      this.save();
    },
    save() {
      if (this.article.id != "") {
        this.submit();
      } else {
        this.dialogVisible = true;
      }
    },
    // 提交
    submit() {
      var avalidate = true;
      this.$refs["form"].validate((valid) => {
        avalidate = valid;
      });
      if (!avalidate) {
        return false;
      }
      this.dialogVisible = false;
      var article = _.cloneDeep(this.article);
      article.content = this.content;
      if (this.saveAction == 2) {
        article.contentPublish = this.content;
        article.publishStatus = 1;
      }
      article.tags = [];
      _.forEach(this.article.tags, function (item) {
        article.tags.push({ id: item });
      });
      this.axios
        .post("/blogbackend/article/save", article)
        .then((response) => {
          this.saveAction = 0;
          if (response.data.success) {
            this.$message({
              showClose: true,
              message: this.optMessage,
              type: "success",
            });
          } else {
            this.$message({
              showClose: true,
              message: response.data.message,
              type: "error",
            });
            return;
          }
          this.article.id = response.data.data;
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    classifySelectChange(classifyId) {
      this.$http({
        url: "/blogbackend/tag/loadTags",
        params: { classifyId: classifyId },
        method: "get",
      }).then((response) => (this.tags = response.data.data));
    },
    loadClassify() {
      this.$http({
        url: "/blogbackend/classify/loadClassify",
        method: "get",
      }).then((response) => (this.classify = response.data.data));
    },
  },
  mounted() {
    this.loadClassify();
  },
  computed: {
    optMessage: function () {
      if (this.saveAction === 1) {
        return "保存草稿成功";
      } else {
        return "发布成功";
      }
    },
  },
};
</script>

<style scoped>
#edit .el-main {
  position: relative;
  top: 0px;
  height: 100%;
  overflow: hidden;
}
#edit .el-header .el-button {
  margin-top: 10px;
}
</style>
